package com.android.love.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.love.R;
import com.android.love.adapter.BannerAdapter;
import com.android.love.adapter.ColorformatAdapter;
import com.android.love.db.DatabaseUtil;
import com.android.love.net.AutoTask;
import com.android.love.util.ActivityUtil;
import com.android.love.util.AnimationManager;
import com.android.love.util.FileUtil;
import com.android.love.util.ImageUtils;
import com.android.love.util.LogUtil;
import com.android.love.view.BadgeView;
import com.android.love.view.CirclePageIndicator;
import com.android.love.view.CustomViewPager;
import com.android.love.view.MyGridView;
import com.android.love.view.MyPosterOnClick;
import com.android.love.view.MyScrollView;
import com.android.love.view.MyScrollView.OnScrollStoppedListener;
import com.android.love.view.MyScrollView.OnScrollUpDownListener;
import com.android.love.view.PopupHelper;
import com.android.love.view.ScrollViewX;
import com.android.love.view.ScrollViewX.OnScrollListener;
import com.android.love.vo.Goods;
import com.android.love.vo.Order;
import com.android.love.vo.SingleGoods;
import com.android.love.vo.TopGoods;
import com.android.love.web.CustomWebView;
import com.android.love.web.EventController;
import com.github.kevinsawicki.wishlist.ViewFinder;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class DetailActivity extends BasePageActivity implements OnScrollListener {

    // private MyPosterView postView;
    private ViewFinder detailFider;

    private Goods goods;

    /** 推荐商品列表 **/
    private List<Goods> goodList;

    /** 顶部滚动商品列表 **/
    private List<TopGoods> topGoods;

    private List<String> images;

    /** 描述图片 **/
    private String desc_img;

    private MyGridView gv_color;

    private MyGridView gv_format;

    // private LazyScrollView sv_detai;
    private ScrollViewX sv_detai;

    private LinearLayout ll_detail_bottom;

    private RelativeLayout rl_detail_opt;

    private TextView tv_detail_opt_hide;

    private ImageView iv_detail_opt_hide_arrow;

    private TextView tv_select;

    private TextView tv_detail_opt_show;

    private TextView tv_format;

    // private ImageView iv_back_img;
    // private ImageView iv_detail_arrow;
    private LinearLayout ll_desc_img;

    private RelativeLayout rl_detail;

    private MyGridView gv_size;

    private TextView tv_size;

    private TextView tv_detail_desc;

    // private CustomWebView wv_detail_desc;

    private String desc_text;

    private TextView tv_goods_id;

    /** 选定size，format，color **/
    private String size;

    private String format;

    private String color;

    private BadgeView badge;

    /** 均码 **/
    private boolean junma=false;

    private AutoTask mAutoTask;

    private CustomViewPager mPager;

    private CirclePageIndicator mIndicator;

    // 要展示的界面
    private List<View> views=null;

    private boolean showFormat;

    // /** 轮转大图图片集合 **/
    // private List<String> topImages = Collections.emptyList();;

    private BannerAdapter mAdapter;

    private ColorformatAdapter cAdapter;

    private ColorformatAdapter sAdapter;

    private ColorformatAdapter fAdapter;

    private Handler handler=new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch(msg.what) {
                case VIEWPAGER:
                    if(mPager.getCurrentItem() < mPager.getAdapter().getCount() - 1)
                        mPager.setCurrentItem(mPager.getCurrentItem() + 1);
                    else
                        mPager.setCurrentItem(0);
                    break;

                default:
                    break;
            }
        };
    };

    private int type;

    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View detailView=inflater.inflate(R.layout.activity_shop_detail, null);
        setContentView(detailView);
        detailFider=new ViewFinder(detailView);
        process();
        setListener();
        LogUtil.i(TAG, "onCreate");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        LogUtil.i(TAG, "onNewIntent");
    }

    @Override
    protected void onResume() {
        mApplication.setDefaultIndex(HOME);
        updateCartNum();
        super.onResume();
        LogUtil.i(TAG, "onResume");
    }

    @Override
    protected Boolean loadBottomTab() {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * 
     * @param tops
     */
    private void updateTop(List<String> tops) {
        // postView.setData(tops, this, true, 5, true,
        // R.drawable.detail_swicth_tip_bg, false);
        if(mAutoTask != null)
            mAutoTask.stop();
        // if(tops == null || tops.size() == 0)
        // return;
        // if(views != null){
        // views.clear();
        // mPager.removeAllViews();
        // mPager.clearAnimation();
        // mPager.removeAllViewsInLayout();
        // mAdapter.notifyDataSetChanged();
        // mIndicator.notifyDataSetChanged();
        // views=null;
        // }
        // tops.clear();
        for(int i=0; i < tops.size(); i++) {
            // final int position=i;
            ImageView img=new ImageView(context);
            LinearLayout ll=new LinearLayout(context);
            ll.addView(img);
            loader.displayImageForSize(tops.get(i), img, ImageUtils.getOptions(R.drawable.zw3), new SimpleImageLoadingListener(),
                0, 0, 0, 0, ActivityUtil.getScreenSize()[0], ActivityUtil.dip2px(context, 300), 1f, true);
            // loader.displayImage(topImages.get(i), img);
            views.add(ll);

        }
        mAdapter.notifyDataSetChanged();
        mIndicator.notifyDataSetChanged();
        mAutoTask=new AutoTask(mPager, handler);
        // mAutoTask.execute(new Void[0]);

        //
    }

    /**
     * 设置顶部轮转大图
     */
    private void setTopPager() {
        views=new ArrayList<View>();
        mAdapter=new BannerAdapter(this, views);
        mPager=(CustomViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        mIndicator=(CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        final float density=getResources().getDisplayMetrics().density;
        // mIndicator.setBackgroundResource(R.drawable.detail_swicth_tip_bg);
        mIndicator.setRadius(3 * density);
        mIndicator.setFillColor(0xFFE2017B);
        mIndicator.setPageColor(0xEECCCCCC);
        mIndicator.setStrokeColor(0xFF000000);
        mIndicator.setStrokeWidth(0 * density);
    }

    @Override
    protected void process() {
        super.process();
        // postView = (MyPosterView) detailFider.find(R.id.detail_Poster);
        // setTopPager();
        sv_detai=detailFider.find(R.id.sv_detai);
        // sv_detai.setOnScrollUpDownListener(this);
        sv_detai.setOnScrollListener(this);
        // ImageView iv_shangyikuan=detailFider.find(R.id.iv_shangyikuan);
        // ImageView iv_xiayikuan=detailFider.find(R.id.iv_xiayikuan);
        sv_detai=detailFider.find(R.id.sv_detai);
        detailFider.setText(R.id.top_title, "宝贝详情");
        detailFider.setDrawable(R.id.iv_shuaxin, R.drawable.btn_detail_top_cart);
        ImageView iv_shuaxin=detailFider.find(R.id.iv_shuaxin);
        LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)iv_shuaxin.getLayoutParams();
        params.rightMargin=ActivityUtil.dip2px(context, 25);
        iv_shuaxin.setLayoutParams(params);
        ll_detail_bottom=detailFider.find(R.id.ll_detail_bottom);
        rl_detail_opt=detailFider.find(R.id.rl_detail_opt);
        gv_color=detailFider.find(R.id.gv_color);
        gv_format=detailFider.find(R.id.gv_format);
        gv_size=detailFider.find(R.id.gv_size);
        tv_detail_opt_hide=detailFider.find(R.id.tv_detail_opt_hide);
        iv_detail_opt_hide_arrow=detailFider.find(R.id.iv_detail_opt_hide_arrow);
        tv_select=detailFider.find(R.id.tv_select);
        tv_format=detailFider.find(R.id.tv_format);
        tv_size=detailFider.find(R.id.tv_size);
        // iv_back_img = detailFider.find(R.id.iv_back_img);
        // iv_detail_arrow = detailFider.find(R.id.iv_detail_arrow);
        tv_detail_opt_show=detailFider.find(R.id.tv_detail_opt_show);
        ll_desc_img=detailFider.find(R.id.ll_desc_img);
        rl_detail=detailFider.find(R.id.rl_detail);
        tv_detail_desc=detailFider.find(R.id.tv_detail_desc);
        // wv_detail_desc=detailFider.find(R.id.wv_detail_desc);
        tv_goods_id=detailFider.find(R.id.tv_goods_id);
        Intent intent=getCurrentIntent();
        if(intent != null) {
            type=intent.getIntExtra("type", 1);
            position=intent.getIntExtra("position", 0);
            if(type == 1) {
                Goods singleGoods=(Goods)intent.getSerializableExtra("singleGoods");
                if(singleGoods == null) {

                    goodList=(List<Goods>)intent.getSerializableExtra("goods");
                    goods=goodList.get(position);
                } else {
                    goods=singleGoods;
                    hide(detailFider.find(R.id.iv_shangyikuan)).hide(detailFider.find(R.id.iv_xiayikuan));
                }
            } else {
                topGoods=(List<TopGoods>)intent.getSerializableExtra("goods");
                goods=topGoods.get(position);
            }
            updateLayout();
            // goods=(Goods)intent.getSerializableExtra("goods");
            // goods=

            // desc_img=goods.getDesc_image();
            // desc_text=goods.getDesc_text();
            // images=goods.getImages_item();
            // detailFider.setText(R.id.tv_name, goods.getGoods_name());
            // detailFider.setText(R.id.tv_price, "¥ " + goods.getPrice());
            // detailFider.setText(R.id.tv_old_price, "¥ " +
            // goods.getOld_price());
            // detailFider.textView(R.id.tv_old_price).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG
            // | Paint.ANTI_ALIAS_FLAG);
            // updateTop(images);
            // LogUtil.i(TAG, "goods.getColor()--" + goods.getColor().size());
            // LogUtil.i(TAG, "goods.getFormat()--" + goods.getFormat().size());
            // if(goods.getColor().size() == 0 && goods.getSize().size() == 0 &&
            // goods.getFormat().size() == 0)
            // junma=true;
            // if(goods.getColor().size() == 0)
            // color="";
            // if(goods.getSize().size() == 0)
            // size="";
            // if(goods.getFormat().size() == 0)
            // format="";
            // if(goods.getColor().size() > 0) {
            // cAdapter=new ColorformatAdapter(goods.getColor(), context, -1);
            // gv_color.setAdapter(cAdapter);
            // if(goods.getColor().size() == 1)
            // tv_detail_opt_hide.setText("颜色分类:" + goods.getColor().get(0));
            // if(goods.getColor().size() >= 2)
            // tv_detail_opt_hide.setText("颜色分类:" + goods.getColor().get(0) +
            // "," + goods.getColor().get(1));
            //
            // } else {
            // tv_detail_opt_hide.setText("颜色分类");
            // }
            // if(goods.getSize().size() > 0) {
            // sAdapter=new ColorformatAdapter(goods.getSize(), context, -1);
            // gv_size.setAdapter(sAdapter);
            // }
            // if(goods.getFormat().size() > 0) {
            //
            // fAdapter=new ColorformatAdapter(goods.getFormat(), context, -1);
            // gv_format.setAdapter(fAdapter);
            // }
            // if(!"".equals(desc_text.trim())) {
            // tv_detail_desc.setText(Html.fromHtml(desc_text));
            // hide(detailFider.imageView(R.id.iv_desc_image));
            // } else {
            // loader.displayImageForSize(desc_img,
            // detailFider.imageView(R.id.iv_desc_image),
            // ImageUtils.getOptions(R.drawable.zw4), new
            // SimpleImageLoadingListener(), 5, 5, 5, 5, 0, 0, 1f, false);
            // hide(tv_detail_desc);
            // }
            // tv_goods_id.setText("(宝贝编号：" + goods.getGoods_id() + ")");
            // showFormat();
        } else {
            finish(true);
        }
        // show(tv_detail_opt_hide).hide(tv_select).hide(tv_detail_opt_show)
        // .hide(gv_color).hide(tv_format).hide(gv_format).hide(tv_size)
        // .hide(gv_size);
    }

    @Override
    protected void setListener() {
        super.setListener();
        detailFider.find(R.id.iv_detail_contact).setOnClickListener(this);
        detailFider.find(R.id.iv_detail_jion_cart).setOnClickListener(this);
        detailFider.find(R.id.iv_detail_purchase).setOnClickListener(this);
        detailFider.find(R.id.iv_detail_purchase_popup).setOnClickListener(this);
        detailFider.find(R.id.iv_detail_jion_cart_popup).setOnClickListener(this);
        detailFider.find(R.id.rl_detail_opt).setOnClickListener(this);
        // detailFider.find(R.id.rl_detail).setOnClickListener(this);
        // detailFider.find(R.id.iv_back_img).setOnClickListener(this);
        detailFider.find(R.id.iv_top_meinv).setOnClickListener(this);
        detailFider.find(R.id.iv_detail_jion_cart).setOnClickListener(this);
        detailFider.find(R.id.iv_detail_purchase).setOnClickListener(this);
        detailFider.find(R.id.iv_shuaxin).setOnClickListener(this);
        detailFider.find(R.id.iv_shangyikuan).setOnClickListener(this);
        detailFider.find(R.id.iv_xiayikuan).setOnClickListener(this);
        gv_size.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                size=goods.getSize().get(position);
                sAdapter.setPos(position);
                sAdapter.notifyDataSetChanged();
            }
        });
        gv_color.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                color=goods.getColor().get(position);
                cAdapter.setPos(position);
                cAdapter.notifyDataSetChanged();
            }
        });
        gv_format.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                format=goods.getFormat().get(position);
                fAdapter.setPos(position);
                fAdapter.notifyDataSetChanged();
            }
        });

        // sv_detai.seto
        // sv_detai.setOnScrollListener(new OnScrollListener() {
        //
        // @Override
        // public void onTop() {
        // LogUtil.i(TAG, "onTop");
        // }
        //
        // @Override
        // public void onScroll() {
        // LogUtil.i(TAG, "onScroll");
        // }
        //
        // @Override
        // public void onBottom() {
        // LogUtil.i(TAG, "onBottom");
        // }
        //
        // @Override
        // public void onAutoScroll(int l, int t, int oldl, int oldt) {
        // // TODO Auto-generated method stub
        // // LogUtil.i(TAG, "onAutoScroll");
        // LogUtil.d(TAG,
        // String.format("%d  %d  %d  %d", l, t, oldl, oldt));
        //
        // // Log.d("MainActivity", "range:" + range);
        // // Log.d("MainActivity", "range-t:" + (range - t));
        // int scrollHeight = t;
        // int scroll_height = sv_detai.getMeasuredHeight();
        // LogUtil.d(TAG, "scroll_height:" + sv_detai);
        //
        // int longHeight = sv_detai.getChildAt(0).getMeasuredHeight();
        // LogUtil.d(TAG, "height:" + longHeight);
        //
        // // 当滚到底部时 马上加载
        // if (scrollHeight + scroll_height >= longHeight - 40) {
        // LogUtil.d(TAG, "底部。。。。。。。。。。。");
        // // show bottom tip
        // if (ll_detail_bottom.getVisibility() == View.GONE) {
        // Animation mBottomBarShowAnimation = AnimationManager
        // .getInstance().getBottomBarShowAnimation();
        // ll_detail_bottom
        // .startAnimation(mBottomBarShowAnimation);
        // ll_detail_bottom.setVisibility(View.VISIBLE);
        // }
        // } else {
        // // hide bottom tip
        // if (ll_detail_bottom.getVisibility() == View.VISIBLE) {
        // Animation mBottomBarHideAnimation = AnimationManager
        // .getInstance().getBottomBarHideAnimation();
        // ll_detail_bottom
        // .startAnimation(mBottomBarHideAnimation);
        // ll_detail_bottom.setVisibility(View.GONE);
        // }
        // }
        // }
        // });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch(v.getId()) {
            case R.id.iv_shangyikuan:
                // 上一款
                if(type == 1) {
                    if(position != 0) {
                        position--;
                        goods=goodList.get(position);
                        updateLayout();
                    } else {
                        ActivityUtil.show(DetailActivity.this, "已经是第一款商品");
                    }
                } else {
                    if(position != 0) {
                        position--;
                        goods=topGoods.get(position);
                        updateLayout();
                    } else {
                        ActivityUtil.show(DetailActivity.this, "已经是第一款商品");
                    }
                }
                break;
            case R.id.iv_xiayikuan:
                // 下一款

                if(type == 1) {
                    if(position != goodList.size() - 1) {
                        position++;
                        goods=goodList.get(position);
                        updateLayout();
                    } else {
                        ActivityUtil.show(DetailActivity.this, "已经是最后一款商品");
                    }
                } else {
                    if(position != topGoods.size() - 1) {
                        position++;
                        goods=topGoods.get(position);
                        updateLayout();
                    } else {
                        ActivityUtil.show(DetailActivity.this, "已经是最后一款商品");
                    }
                }
                break;
            case R.id.iv_top_meinv:
                finish(true);
                break;
            case R.id.iv_detail_jion_cart_popup:
            case R.id.iv_detail_jion_cart:
                // 加入购物车
                addCart();
                break;
            case R.id.iv_detail_purchase:
            case R.id.iv_detail_purchase_popup:
                // 立即购买
                purchase();
                break;
            case R.id.iv_shuaxin:
                // 购物车
                if(DatabaseUtil.getInstance(context).queryCart().size() != 0) {
                    Intent cartIntent=new Intent(context, CartActivity.class);
                    startActivity(cartIntent);
                } else {
                    ActivityUtil.show(DetailActivity.this, "购物车没有宝贝，添加一个吧");
                }
                break;
            case R.id.iv_detail_contact:
                showPopupWindow(ll_detail_bottom, 3);
                break;
            // case R.id.rl_detail:
            // // showPopupWindow();
            // if (ll_desc_img.getVisibility() == View.VISIBLE) {
            // detailFider.setDrawable(R.id.iv_detail_arrow,
            // R.drawable.detail_arrow);
            // hide(ll_desc_img);
            // } else {
            // detailFider.setDrawable(R.id.iv_detail_arrow,
            // R.drawable.detail_arrow_up);
            // show(ll_desc_img);
            // }
            // break;
            case R.id.rl_detail_opt:
                showFormat(true);
                break;
            // case R.id.iv_back_img:
            // show(tv_detail_opt_hide).show(iv_detail_opt_hide_arrow)
            // .hide(tv_select).hide(tv_detail_opt_show).hide(gv_color)
            // .hide(tv_format).hide(gv_format).hide(tv_size)
            // .hide(gv_size);
            // break;

            default:
                break;
        }
    }

    /**
     * 立即购买
     */
    private void purchase() {
        if(!junma && (color == null || size == null || format == null)) {
            if(showFormat)
                showFormat(true);
            ActivityUtil.show(DetailActivity.this, "请选择对应规格颜色");
            return;
        }
        Intent orderIntent=new Intent(context, OrderActivity.class);
        orderIntent.putExtra("single", true);
        Order order=new Order();
        order.setSum(goods.getPrice() + 10f);
        List<SingleGoods> sGoods=new ArrayList<SingleGoods>();
        SingleGoods sGood=new SingleGoods();
        sGood.setAttribute(color + " " + size + " " + format);
        sGood.setCount(1);
        sGood.setGoods_image(goods.getGoods_image());
        sGood.setGoods_name(goods.getGoods_name());
        sGood.setGoods_id(goods.getGoods_id());
        sGood.setPrice(goods.getPrice());
        sGoods.add(sGood);
        order.setGoods(sGoods);
        orderIntent.putExtra("order", order);
        startActivityForResult(orderIntent, TIJIAO);
        overridePendingTransition(R.anim.slide_up_in, R.anim.hold);
    }

    /**
     * 加入购物车
     */
    private void addCart() {
        if(!junma && (color == null || size == null || format == null)) {
            if(showFormat)
                showFormat(true);
            ActivityUtil.show(DetailActivity.this, "请选择对应规格颜色");
            return;
        }
        boolean exit=DatabaseUtil.getInstance(context).isExitCart(goods.getGoods_id());
        if(exit) {
            ActivityUtil.show(DetailActivity.this, "该商品已加入购物车，请勿重复添加 ");
        } else {
            boolean result=DatabaseUtil.getInstance(context).operateFrovite(goods, true, size, color, format);
            if(result) {
                ActivityUtil.show(DetailActivity.this, "成功加入购物车");
                // MyHandler.getInstance().postDelayed(new Runnable() {
                //
                // @Override
                // public void run() {
                updateCartNum();
                updateBottomCart();
                // }
                // }, 300);
            } else
                ActivityUtil.show(DetailActivity.this, "加入购物车失败,请重新操作");
        }
    }

    /**
     * 展示size，color，format
     */
    private void showFormat(boolean flag) {
        if(flag) {
            if(showFormat) {
                showFormat=false;
                iv_detail_opt_hide_arrow.setImageResource(R.drawable.btn_jiantou_up);
                if(goods.getColor() == null || goods.getColor().size() == 0)
                    hide(tv_detail_opt_show).hide(gv_color);
                else
                    show(tv_detail_opt_show).show(gv_color);
                if(goods.getSize() == null || goods.getSize().size() == 0)
                    hide(tv_size).hide(gv_size);
                else
                    show(tv_size).show(gv_size);
                if(goods.getFormat() == null || goods.getFormat().size() == 0)
                    hide(tv_format).hide(gv_format);
                else
                    show(tv_format).show(gv_format);
                if(goods.getColor().size() == 0 && goods.getSize().size() == 0 && goods.getFormat().size() == 0) {
                    ActivityUtil.show(DetailActivity.this, "该商品为均码");
                } else {
                    hide(tv_detail_opt_hide).show(tv_select);
                }

            } else {
                showFormat=true;
                iv_detail_opt_hide_arrow.setImageResource(R.drawable.btn_jiantou_down);
                show(tv_detail_opt_hide).hide(tv_select).hide(tv_detail_opt_show).hide(gv_color).hide(tv_format).hide(gv_format)
                    .hide(tv_size).hide(gv_size);
            }
        } else {
            iv_detail_opt_hide_arrow.setImageResource(R.drawable.btn_jiantou_down);
            show(tv_detail_opt_hide).hide(tv_select).hide(tv_detail_opt_show).hide(gv_color).hide(tv_format).hide(gv_format)
                .hide(tv_size).hide(gv_size);
        }
    }

    private void updateLayout() {
        desc_img=goods.getDesc_image();
        desc_text=goods.getDesc_text();
        images=goods.getImages_item();
        detailFider.setText(R.id.tv_name, goods.getGoods_name());
        detailFider.setText(R.id.tv_price, "¥ " + goods.getPrice());
        detailFider.setText(R.id.tv_old_price, "¥ " + goods.getOld_price());
        detailFider.textView(R.id.tv_old_price).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        setTopPager();
        updateTop(images);
        LogUtil.i(TAG, "goods.getColor()--" + goods.getColor().size());
        LogUtil.i(TAG, "goods.getFormat()--" + goods.getFormat().size());
        if(goods.getColor().size() == 0 && goods.getSize().size() == 0 && goods.getFormat().size() == 0)
            junma=true;
        if(goods.getColor().size() == 0)
            color="";
        if(goods.getSize().size() == 0)
            size="";
        if(goods.getFormat().size() == 0)
            format="";
        if(goods.getColor().size() > 0) {
            cAdapter=new ColorformatAdapter(goods.getColor(), context, -1);
            gv_color.setAdapter(cAdapter);
            if(goods.getColor().size() == 1)
                tv_detail_opt_hide.setText("颜色分类:" + goods.getColor().get(0));
            if(goods.getColor().size() >= 2)
                tv_detail_opt_hide.setText("颜色分类:" + goods.getColor().get(0) + "," + goods.getColor().get(1));

        } else {
            tv_detail_opt_hide.setText("颜色分类");
        }
        if(goods.getSize().size() > 0) {
            sAdapter=new ColorformatAdapter(goods.getSize(), context, -1);
            gv_size.setAdapter(sAdapter);
        }
        if(goods.getFormat().size() > 0) {

            fAdapter=new ColorformatAdapter(goods.getFormat(), context, -1);
            gv_format.setAdapter(fAdapter);
        }
        if("".equals(desc_img.trim())) {
            tv_detail_desc.setText(desc_text);
            // wv_detail_desc.clearView();
            // wv_detail_desc.requestLayout();
            // wv_detail_desc.loadUrl("file://" + FileUtil.saveTempFile(goods, context));
            // hide(detailFider.imageView(R.id.iv_desc_image));
            hide(detailFider.find(R.id.iv_desc_image));
            show(tv_detail_desc);
            // wv_detail_desc.reload();
        } else {
            loader.displayImageForSize(desc_img, detailFider.imageView(R.id.iv_desc_image), ImageUtils.getOptions(R.drawable.zw4),
                new SimpleImageLoadingListener(), 5, 5, 5, 5, 0, 0, 1f, false);
            show(detailFider.find(R.id.iv_desc_image));
            hide(tv_detail_desc);
        }
        tv_goods_id.setText("(宝贝编号：" + goods.getGoods_id() + ")");
        showFormat=true;
        showFormat(false);
    }

    /**
     * 更新购物车数量提醒
     */
    private void updateCartNum() {
        List<Goods> lists=DatabaseUtil.getInstance(context).queryCart();
        if(lists != null && lists.size() > 0) {
            if(badge == null)
                badge=new BadgeView(this, findViewById(R.id.ll_right));
            LogUtil.i(TAG, "lists.size()---" + lists.size());
            badge.setText(lists.size() + "");
            badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
            badge.setBadgeBackgroundColor(getResources().getColor(R.color.white));
            badge.setTextColor(getResources().getColor(R.color.pink));
            badge.show(true);
        } else {
            if(badge != null)
                badge.hide();
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            finish(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
        if(arg0 == TIJIAO) {
            if(arg2 != null) {
                boolean result=arg2.getBooleanExtra("result", false);
                if(result) {
                    finish(true);
                    Order order=(Order)arg2.getSerializableExtra("order");
                    Intent intent=new Intent(context, MyOrderDetail_1.class);
                    intent.putExtra("order", order);
                    intent.putExtra("show", true);
                    startActivityForResult(intent, 0);
                }
            }
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        // if (mAutoTask != null)
        // mAutoTask.stop();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        // if (mAutoTask != null)
        // mAutoTask.stop();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(mAutoTask != null) {
            mAutoTask.stop();
        }
        // if (wv_detail_desc != null) {
        // sv_detai.removeAllViews();
        // wv_detail_desc.removeAllViews();
        // // wv_detail_desc.clearCache(true);
        // wv_detail_desc.destroy();
        // wv_detail_desc = null;
        // Constant.PURCHASE_FROM_MAIN=true;
        // }
    }

    // @Override
    // public void onScrollUp() {
    // // hide bottom tip
    // if (ll_detail_bottom.getVisibility() == View.VISIBLE) {
    // Animation mBottomBarHideAnimation = AnimationManager.getInstance()
    // .getBottomBarHideAnimation();
    // ll_detail_bottom.startAnimation(mBottomBarHideAnimation);
    // ll_detail_bottom.setVisibility(View.GONE);
    // }
    // }
    //
    // @Override
    // public void onScrollDown() {
    // // show bottom tip
    // if (ll_detail_bottom.getVisibility() == View.GONE) {
    // Animation mBottomBarShowAnimation = AnimationManager.getInstance()
    // .getBottomBarShowAnimation();
    // ll_detail_bottom.startAnimation(mBottomBarShowAnimation);
    // ll_detail_bottom.setVisibility(View.VISIBLE);
    // }
    // }

    @Override
    public void onScrollStopped() {
        if(sv_detai.isAtBottom()) {
            // show bottom tip
            if(ll_detail_bottom.getVisibility() == View.GONE) {
                Animation mBottomBarShowAnimation=AnimationManager.getInstance().getBottomBarShowAnimation();
                ll_detail_bottom.startAnimation(mBottomBarShowAnimation);
                ll_detail_bottom.setVisibility(View.VISIBLE);
            }
        } else if(sv_detai.isAtTop()) {
            // hide bottom tip
            if(ll_detail_bottom.getVisibility() == View.VISIBLE) {
                Animation mBottomBarHideAnimation=AnimationManager.getInstance().getBottomBarHideAnimation();
                ll_detail_bottom.startAnimation(mBottomBarHideAnimation);
                ll_detail_bottom.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onScrolling() {
        // // hide bottom tip
        // if (ll_detail_bottom.getVisibility() == View.VISIBLE) {
        // Animation mBottomBarHideAnimation = AnimationManager.getInstance()
        // .getBottomBarHideAnimation();
        // ll_detail_bottom.startAnimation(mBottomBarHideAnimation);
        // ll_detail_bottom.setVisibility(View.GONE);
        // }
    }

    @Override
    public void onScrollChanged(int x, int y, int oldX, int oldY) {
        // TODO Auto-generated method stub

    }
}
